'use strict';

var debug = require('debug'),
  log = debug('yozh:lib-run:log'),
  error = debug('yozh:lib-run:error'),
  verbose = debug('yozh:lib-run:verbose');

/**
 * run callbace function with ES6 generator function
 * run.xxx(function *(resume){...})
 * @param  {Object} opts options
 * @return {Object} object to run callbace function with ES6 generator function
 */
module.exports = exports = function runFunc(opts) {
  var run = {};
  var res = {};
  /**
   * run the callback function like this:
   * run.runWebotReply(function *(resume) {
   *   yield webot.reply(opts, resume);
   * })
   * @param  {[type]} generatorFunction [description]
   * @return {[type]}                   [description]
   */
  run.runWebotReply = function(generatorFunction) {
    var resume = function(err, info) {
      // verbose('2run: ', err, info);
      res = {err: err, info: info};
      // var genRes = generatorItr.next(err, info);
      // verbose('2:', genRes);
      // return res;
    };
    var generatorItr = generatorFunction(resume);
    // verbose('1:', resume, generatorItr);

    var genRes = generatorItr.next();
    // verbose('3:', genRes.value, genRes.value.WeBot,
    //  genRes.value.Webot, genRes.value.Rule, genRes.value.handler,
    //  genRes.value.Info, res);
    if (!res.info && 0) {
      this.runWebotReply(generatorFunction);
    } else {
      genRes = generatorItr.next();
      // verbose('4:', genRes, res);
      return res;
    }
    // return res;
  };
  run.runWebotHandler = function(generatorFunction) {
    var resume = function(err, info) {
      // verbose('2run: ', err, info);
      res = {err: err, info: info};
      // var genRes = generatorItr.next(err, info);
      // verbose('2:', genRes);
    };
    var generatorItr = generatorFunction(resume);
    // verbose('1:', resume, generatorItr);

    var genRes = generatorItr.next();
    // verbose('3:', genRes.value, genRes.value.WeBot,
    //  genRes.value.Webot, genRes.value.Rule, genRes.value.handler,
    //  genRes.value.Info, res);
    // if (!res.info && 0) {
    //   this.runWebotReply(generatorFunction);
    // } else {
    //   genRes = generatorItr.next();
    //   verbose('4:', genRes, res);
    //   return res;
    // }
    return res;
  };
  return run;
};
